<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>魔术方法</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.oop5.iterations.html">« 遍历对象</a></li>
      <li style="float: right;"><a href="language.oop5.final.html">Final 关键字 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.oop5.html">类与对象</a></li>
    <li>魔术方法</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.oop5.magic" class="sect1">
  <h2 class="title">魔术方法</h2>
  <p class="para">
   魔术方法是一种特殊的方法，当对对象执行某些操作时会覆盖 PHP 的默认操作。
  </p>
  <div class="caution"><strong class="caution">警告</strong>
   <p class="simpara">
    PHP 保留所有以 <code class="literal">__</code> 开头的方法名称。
    因此，除非覆盖 PHP 的行为，否则不建议使用此类方法名称。
   </p>
  </div>
  <p class="para">
   下列方法名被认为是魔术方法：
   
   <a href="language.oop5.decon.html#object.construct" class="link">__construct()</a> 、
   <a href="language.oop5.decon.html#object.destruct" class="link">__destruct()</a> 、
   <a href="language.oop5.overloading.html#object.call" class="link">__call()</a> 、
   <a href="language.oop5.overloading.html#object.callstatic" class="link">__callStatic()</a> 、
   <a href="language.oop5.overloading.html#object.get" class="link">__get()</a> 、
   <a href="language.oop5.overloading.html#object.set" class="link">__set()</a> 、
   <a href="language.oop5.overloading.html#object.isset" class="link">__isset()</a> 、
   <a href="language.oop5.overloading.html#object.unset" class="link">__unset()</a> 、
   <a href="language.oop5.magic.html#object.sleep" class="link">__sleep()</a> 、
   <a href="language.oop5.magic.html#object.wakeup" class="link">__wakeup()</a> 、
   <a href="language.oop5.magic.html#object.serialize" class="link">__serialize()</a> 、
   <a href="language.oop5.magic.html#object.unserialize" class="link">__unserialize()</a> 、
   <a href="language.oop5.magic.html#object.tostring" class="link">__toString()</a> 、
   <a href="language.oop5.magic.html#object.invoke" class="link">__invoke()</a> 、
   <a href="language.oop5.magic.html#object.set-state" class="link">__set_state()</a> 、
   <a href="language.oop5.cloning.html#object.clone" class="link">__clone()</a> 、
   <a href="language.oop5.magic.html#object.debuginfo" class="link">__debugInfo()</a> 。
  </p>

  <div class="warning"><strong class="warning">警告</strong>
   
   <p class="simpara">
    除了 <a href="language.oop5.decon.html#object.construct" class="link">__construct()</a>，
    <a href="language.oop5.decon.html#object.destruct" class="link">__destruct()</a> ，和
    <a href="language.oop5.cloning.html#object.clone" class="link">__clone()</a> 之外的所有魔术方法都
    <em class="emphasis">必须</em> 声明为 <code class="literal">public</code>，
    否则会发出 <strong><code>E_WARNING</code></strong>。
    在 PHP 8.0.0 之前没有为魔术方法
    <a href="language.oop5.magic.html#object.sleep" class="link">__sleep()</a> 、
    <a href="language.oop5.magic.html#object.wakeup" class="link">__wakeup()</a> 、
    <a href="language.oop5.magic.html#object.serialize" class="link">__serialize()</a> 、
    <a href="language.oop5.magic.html#object.unserialize" class="link">__unserialize()</a> 、
    <a href="language.oop5.magic.html#object.set-state" class="link">__set_state()</a> 发出诊断信息。
   </p>
  </div>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    如果定义魔术方法时使用类型声明，它们必须与本文档中描述的签名相同，否则会发出致命错误。
    在 PHP 8.0.0 之前，不会发出诊断信息。
    然而， <a href="language.oop5.decon.html#object.construct" class="link">__construct()</a> 和
    <a href="language.oop5.decon.html#object.destruct" class="link">__destruct()</a> 不能声明返回类型，
    否则会发出致命错误。
   </p>
  </div>
 
  <div class="sect2" id="language.oop5.magic.sleep">
   <h3 class="title">
    <a href="language.oop5.magic.html#object.sleep" class="link">__sleep()</a> 和
    <a href="language.oop5.magic.html#object.wakeup" class="link">__wakeup()</a>
   </h3>
   
   <div class="methodsynopsis dc-description" id="object.sleep">
    <span class="modifier">public</span> <span class="methodname"><strong>__sleep</strong></span>(): <span class="type">array</span></div>

   <div class="methodsynopsis dc-description" id="object.wakeup"><span class="modifier">public</span> <span class="methodname"><strong>__wakeup</strong></span>(): <span class="type"><span class="type void">void</span></span></div>

   
   <p class="para">
    <span class="function"><a href="function.serialize.html" class="function">serialize()</a></span> 函数会检查类中是否存在一个魔术方法
    <a href="language.oop5.magic.html#object.sleep" class="link">__sleep()</a>。如果存在，该方法会先被调用，然后才执行序列化操作。此功能可以用于清理对象，并返回一个包含对象中所有应被序列化的变量名称的数组。如果该方法未返回任何内容，则
    <strong><code>null</code></strong> 被序列化，并产生一个 <strong><code>E_NOTICE</code></strong> 级别的错误。
    </p>
   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
     <a href="language.oop5.magic.html#object.sleep" class="link">__sleep()</a> 不能返回父类的私有成员的名字。这样做会产生一个
     <strong><code>E_NOTICE</code></strong> 级别的错误。使用
     <a href="language.oop5.magic.html#object.serialize" class="link">__serialize()</a> 接口替代。
    </p>
   </p></blockquote>
    
   <p class="para">
    <a href="language.oop5.magic.html#object.sleep" class="link">__sleep()</a> 方法常用于提交未提交的数据，或类似的清理操作。同时，如果有一些很大的对象，但不需要全部保存，这个功能就很好用。
   </p>
   <p class="para">
    与之相反，<span class="function"><a href="function.unserialize.html" class="function">unserialize()</a></span> 会检查是否存在一个 <a href="language.oop5.magic.html#object.wakeup" class="link">__wakeup()</a>
    方法。如果存在，则会先调用 <code class="literal">__wakeup</code> 方法，预先准备对象需要的资源。
   </p>
   <p class="para">
    <a href="language.oop5.magic.html#object.wakeup" class="link">__wakeup()</a> 经常用在反序列化操作中，例如重新建立数据库连接，或执行其它初始化操作。
   </p>
   <div class="example" id="example-274">
    <p><strong>示例 #1 Sleep 和 wakeup</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Connection&nbsp;<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;</span><span style="color: #0000BB">$link</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$server</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$username</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$server</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$username</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">server&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$server</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$username</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;function&nbsp;</span><span style="color: #0000BB">connect</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">link&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">server</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">password</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mysql_select_db</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">link</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__sleep</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;array(</span><span style="color: #DD0000">'server'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'username'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'password'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'db'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__wakeup</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </div>

  <div class="sect2" id="language.oop5.magic.serialize">
   <h3 class="title">
    <a href="language.oop5.magic.html#object.serialize" class="link">__serialize()</a> 和
    <a href="language.oop5.magic.html#object.unserialize" class="link">__unserialize()</a>
   </h3>

   <div class="methodsynopsis dc-description" id="object.serialize">
    <span class="modifier">public</span> <span class="methodname"><strong>__serialize</strong></span>(): <span class="type">array</span></div>

   <div class="methodsynopsis dc-description" id="object.unserialize"><span class="modifier">public</span> <span class="methodname"><strong>__unserialize</strong></span>(<span class="methodparam"><span class="type">array</span> <code class="parameter">$data</code></span>): <span class="type"><span class="type void">void</span></span></div>


   <p class="para">
    <span class="function"><a href="function.serialize.html" class="function">serialize()</a></span> 函数会检查类中是否存在一个魔术方法
    <a href="language.oop5.magic.html#object.serialize" class="link">__serialize()</a> 
    。如果存在，该方法将在任何序列化之前优先执行。它必须以一个代表对象序列化形式的 键/值 成对的关联数组形式来返回，如果没有返回数组，将会抛出一个
    <span class="classname"><a href="class.typeerror.html" class="classname">TypeError</a></span> 错误。
   </p>
   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
     如果类中同时定义了 <a href="language.oop5.magic.html#object.serialize" class="link">__serialize()</a> 和 <a href="language.oop5.magic.html#object.sleep" class="link">__sleep()</a>
     两个魔术方法，则只有 <a href="language.oop5.magic.html#object.serialize" class="link">__serialize()</a> 方法会被调用。
     <a href="language.oop5.magic.html#object.sleep" class="link">__sleep()</a> 方法会被忽略掉。如果对象实现了 <a href="class.serializable.html" class="link">Serializable</a>
     接口，接口的 <code class="literal">serialize()</code> 方法会被忽略，做为代替类中的 <a href="language.oop5.magic.html#object.serialize" class="link">__serialize()</a> 方法会被调用。
    </p>
   </p></blockquote>
   <p class="para">
    <a href="language.oop5.magic.html#object.serialize" class="link">__serialize()</a> 的预期用途是定义对象序列化友好的任意表示。
    数组的元素可能对应对象的属性，但是这并不是必须的。
   </p>
   <p class="para">
    相反， <span class="function"><a href="function.unserialize.html" class="function">unserialize()</a></span> 检查是否存在具有名为 
    <a href="language.oop5.magic.html#object.unserialize" class="link">__unserialize()</a>
    的魔术方法。此函数将会传递从 <a href="language.oop5.magic.html#object.serialize" class="link">__serialize()</a>
    返回的恢复数组。然后它可以根据需要从该数组中恢复对象的属性。
   </p>
   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
     如果类中同时定义了 <a href="language.oop5.magic.html#object.unserialize" class="link">__unserialize()</a> 和 <a href="language.oop5.magic.html#object.wakeup" class="link">__wakeup()</a>
     两个魔术方法，则只有 <a href="language.oop5.magic.html#object.unserialize" class="link">__unserialize()</a> 方法会生效，<a href="language.oop5.magic.html#object.wakeup" class="link">__wakeup()</a> 方法会被忽略。
    </p>
   </p></blockquote>
   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <p class="para">
      此特性自 PHP 7.4.0 起可用。
    </p>
   </p></blockquote>
   <div class="example" id="example-275">
    <p><strong>示例 #2 序列化和反序列化</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Connection<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;</span><span style="color: #0000BB">$link</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$dsn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$username</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$dsn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$username</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dsn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dsn</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$username</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;function&nbsp;</span><span style="color: #0000BB">connect</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">link&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dsn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">password</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__serialize</span><span style="color: #007700">():&nbsp;array<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'dsn'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dsn</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'user'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'pass'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">password</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;];<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__unserialize</span><span style="color: #007700">(array&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">void<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dsn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">[</span><span style="color: #DD0000">'dsn'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">[</span><span style="color: #DD0000">'user'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">[</span><span style="color: #DD0000">'pass'</span><span style="color: #007700">];<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}</span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </div>

  <div class="sect2" id="language.oop5.magic.tostring">
   <h3 class="title"><a href="language.oop5.magic.html#object.tostring" class="link">__toString()</a></h3>
   <div class="methodsynopsis dc-description" id="object.tostring">
    <span class="modifier">public</span> <span class="methodname"><strong>__toString</strong></span>(): <span class="type">string</span></div>

   
   <p class="para">
    <a href="language.oop5.magic.html#object.tostring" class="link">__toString()</a> 方法用于一个类被当成字符串时应怎样回应。例如
    <code class="literal">echo $obj;</code> 应该显示些什么。
   </p>
   <div class="warning"><strong class="warning">警告</strong>
    <p class="para">
     从 PHP 8.0.0 起，返回值遵循标准的 PHP 类型语义，
     这意味着如果禁用 <a href="language.types.declarations.html#language.types.declarations.strict" class="link">严格类型</a>
     ，它将会强制转换为字符串。
    </p>
    <p class="para">
     从 PHP 8.0.0 起，任何包含 <a href="language.oop5.magic.html#object.tostring" class="link">__toString()</a>
     方法的类都将隐性实现 <span class="interfacename"><a href="class.stringable.html" class="interfacename">Stringable</a></span> 接口，
     因此将通过该接口的类型检查。推荐无论如何应显式实现该接口。
    </p>
    <p class="para">
     在 PHP 7.4 中，返回值 <em class="emphasis">必须</em> 是 <span class="type">string</span>
     ，否则会抛出 <span class="classname"><a href="class.error.html" class="classname">Error</a></span> 。
    </p>
    <p class="para">
     在 PHP 7.4.0 之前，返回值 <em class="emphasis">必须</em> 是 <span class="type">string</span>
     ，否则会抛出致命错误 <strong><code>E_RECOVERABLE_ERROR</code></strong> 。
    </p>
   </div>
   <div class="warning"><strong class="warning">警告</strong>
    <p class="simpara">
     在 PHP 7.4.0 之前不能在 <a href="language.oop5.magic.html#object.tostring" class="link">__toString()</a>
     方法中抛出异常。这么做会导致致命错误。
    </p>
   </div>
   <div class="example" id="example-276">
    <p><strong>示例 #3 简单示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;声明一个简单的类<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">TestClass<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$foo</span><span style="color: #007700">)&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__toString</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$class&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">TestClass</span><span style="color: #007700">(</span><span style="color: #DD0000">'Hello'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$class</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Hello
</pre></div>
    </div>
   </div>
  </div>

  <div class="sect2" id="language.oop5.magic.invoke">
   <h3 class="title"><a href="language.oop5.magic.html#object.invoke" class="link">__invoke()</a></h3>
   <div class="methodsynopsis dc-description" id="object.invoke">
    <span class="methodname"><strong>__invoke</strong></span>(<span class="methodparam"> <code class="parameter">...$values</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

   <p class="para">
    当尝试以调用函数的方式调用一个对象时，<a href="language.oop5.magic.html#object.invoke" class="link">__invoke()</a> 方法会被自动调用。
   </p>
   <div class="example" id="example-277">
    <p><strong>示例 #4 使用 <a href="language.oop5.magic.html#object.invoke" class="link">__invoke()</a></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">CallableClass&nbsp;<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">__invoke</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">CallableClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$obj</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">is_callable</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
int(5)
bool(true)
</pre></div>
    </div>
   </div>
   <div class="example" id="example-278">
    <p><strong>示例 #5 使用 <a href="language.oop5.magic.html#object.invoke" class="link">__invoke()</a></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Sort<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">string&nbsp;$key</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__invoke</span><span style="color: #007700">(array&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;array&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">int<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">key</span><span style="color: #007700">]&nbsp;&lt;=&gt;&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">[</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">key</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">$customers&nbsp;</span><span style="color: #007700">=&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;[</span><span style="color: #DD0000">'id'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'first_name'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'John'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'last_name'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Do'</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;[</span><span style="color: #DD0000">'id'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'first_name'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Alice'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'last_name'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Gustav'</span><span style="color: #007700">],<br />&nbsp;&nbsp;&nbsp;&nbsp;[</span><span style="color: #DD0000">'id'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'first_name'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Bob'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'last_name'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Filipe'</span><span style="color: #007700">]<br />];<br /></span><span style="color: #FF8000">//&nbsp;sort&nbsp;customers&nbsp;by&nbsp;first&nbsp;name<br /></span><span style="color: #0000BB">usort</span><span style="color: #007700">(</span><span style="color: #0000BB">$customers</span><span style="color: #007700">,&nbsp;new&nbsp;</span><span style="color: #0000BB">Sort</span><span style="color: #007700">(</span><span style="color: #DD0000">'first_name'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$customers</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;sort&nbsp;customers&nbsp;by&nbsp;last&nbsp;name<br /></span><span style="color: #0000BB">usort</span><span style="color: #007700">(</span><span style="color: #0000BB">$customers</span><span style="color: #007700">,&nbsp;new&nbsp;</span><span style="color: #0000BB">Sort</span><span style="color: #007700">(</span><span style="color: #DD0000">'last_name'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$customers</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; Array
        (
            [id] =&gt; 3
            [first_name] =&gt; Alice
            [last_name] =&gt; Gustav
        )
    [1] =&gt; Array
        (
            [id] =&gt; 2
            [first_name] =&gt; Bob
            [last_name] =&gt; Filipe
        )
    [2] =&gt; Array
        (
            [id] =&gt; 1
            [first_name] =&gt; John
            [last_name] =&gt; Do
        )
)
Array
(
    [0] =&gt; Array
        (
            [id] =&gt; 1
            [first_name] =&gt; John
            [last_name] =&gt; Do
        )
    [1] =&gt; Array
        (
            [id] =&gt; 2
            [first_name] =&gt; Bob
            [last_name] =&gt; Filipe
        )
    [2] =&gt; Array
        (
            [id] =&gt; 3
            [first_name] =&gt; Alice
            [last_name] =&gt; Gustav
        )
)
</pre></div>
    </div>
   </div>
  </div>

  <div class="sect2" id="language.oop5.magic.set-state">
   <h3 class="title"><a href="language.oop5.magic.html#object.set-state" class="link">__set_state()</a></h3>
   <div class="methodsynopsis dc-description" id="object.set-state">
    <span class="modifier">static</span> <span class="methodname"><strong>__set_state</strong></span>(<span class="methodparam"><span class="type">array</span> <code class="parameter">$properties</code></span>): <span class="type">object</span></div>

   <p class="para">
    当调用 <span class="function"><a href="function.var-export.html" class="function">var_export()</a></span> 导出类时，此<a href="language.oop5.static.html" class="link">静态</a> 方法会被调用。
   </p>
   <p class="para">
    本方法的唯一参数是一个数组，其中包含按 <code class="literal">[&#039;property&#039; =&gt; value, ...]</code> 格式排列的类属性。
   </p>
   <div class="example" id="example-279">
    <p><strong>示例 #6 使用 <a href="language.oop5.magic.html#object.set-state" class="link">__set_state()</a>&gt;</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">A<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$var1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$var2</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;function&nbsp;</span><span style="color: #0000BB">__set_state</span><span style="color: #007700">(</span><span style="color: #0000BB">$an_array</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">var1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$an_array</span><span style="color: #007700">[</span><span style="color: #DD0000">'var1'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">var2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$an_array</span><span style="color: #007700">[</span><span style="color: #DD0000">'var2'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">var1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">var2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br />eval(</span><span style="color: #DD0000">'$c&nbsp;=&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">';'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(60) &quot;A::__set_state(array(
   &#039;var1&#039; =&gt; 5,
   &#039;var2&#039; =&gt; &#039;foo&#039;,
))&quot;
object(A)#2 (2) {
  [&quot;var1&quot;]=&gt;
  int(5)
  [&quot;var2&quot;]=&gt;
  string(3) &quot;foo&quot;
}
</pre></div>
    </div>
   </div>
   <blockquote class="note"><p><strong class="note">注意</strong>: 
    <span class="simpara">
     导出对象时， <span class="function"><a href="function.var-export.html" class="function">var_export()</a></span> 不会检查对象类是否实现了
     <a href="language.oop5.magic.html#object.set-state" class="link">__set_state()</a> ，
     所以如果 <a href="language.oop5.magic.html#object.set-state" class="link">__set_state()</a> 没有实现，
     重新导入对象会触发 <span class="classname"><a href="class.error.html" class="classname">Error</a></span> 异常。特别是这会影响内部(内置)类。
    </span>
    <span class="simpara">
     程序员有责任验证要重新导入的类是否实现了 
     <a href="language.oop5.magic.html#object.set-state" class="link">__set_state()</a> 。
    </span>
   </p></blockquote>
  </div>
  <div class="sect2" id="language.oop5.magic.debuginfo">
   <h3 class="title"><a href="language.oop5.magic.html#object.debuginfo" class="link">__debugInfo()</a></h3>
   <div class="methodsynopsis dc-description" id="object.debuginfo">
    <span class="methodname"><strong>__debugInfo</strong></span>(): <span class="type">array</span></div>

   <p class="para">
    当通过 <span class="function"><a href="function.var-dump.html" class="function">var_dump()</a></span> 转储对象，获取应该要显示的属性的时候，
    该函数就会被调用。如果对象中没有定义该方法，那么将会展示所有的公有、受保护和私有的属性。
   </p>
   <div class="example" id="example-280">
    <p><strong>示例 #7 使用 <a href="language.oop5.magic.html#object.debuginfo" class="link">__debugInfo()</a></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">C&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$prop</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prop&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__debugInfo</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'propSquared'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prop&nbsp;</span><span style="color: #007700">**&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;];<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">C</span><span style="color: #007700">(</span><span style="color: #0000BB">42</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
object(C)#1 (1) {
  [&quot;propSquared&quot;]=&gt;
  int(1764)
}
</pre></div>
    </div>
   </div>
  </div>
 </div></div></div></body></html>